﻿using Iworld.Author.Model;
using Iworld.basis.Service;
using Iworld.Basis.Help;
using Iworld.Basis.Manager;
using Iworld.Main.Impl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace Iworld.Author.Service.Client.Import
{
    /// <summary>
    /// 注册用户
    /// </summary>
    [DataContract]
    public class RegisterImport : ImportBase, IPackageForCreateModel<IModelToDbContextOfAuthor, Iworld.Author.Model.Author>
    {
        /// <summary>
        /// 账号名称
        /// </summary>
        [DataMember]
        public string Account { set; get; }

        /// <summary>
        /// 用户名
        /// </summary>
        [DataMember]
        public string Username { get; set; }

        /// <summary>
        /// 密码
        /// </summary>
        [DataMember]
        public string Password { get; set; }

        /// <summary>
        /// 二级密码
        /// </summary>
        [DataMember]
        public string TwoLevelPassword { get; set; }

        /// <summary>
        /// QQ号码
        /// </summary>
        [DataMember]
        public string QQ { set; get; }

        /// <summary>
        /// 电话号码
        /// </summary>
        [DataMember]
        public string Phone { set; get; }

        /// <summary>
        /// 身份证号码
        /// </summary>
        [DataMember]
        public string IdentityCard { set; get; }

        public void CheckData(IModelToDbContextOfAuthor db)
        {
            this.Username = VerifyHelper.EliminateSpaces(this.Username);
            this.Password = VerifyHelper.EliminateSpaces(this.Password);
            this.TwoLevelPassword = VerifyHelper.EliminateSpaces(this.TwoLevelPassword);
            VerifyHelper.Check(this.Username, VerifyHelper.Key.Nickname);
            VerifyHelper.Check(this.Password, VerifyHelper.Key.Password);
            VerifyHelper.Check(this.TwoLevelPassword, VerifyHelper.Key.Password);
            bool hasAccount = db.Authors.Any(x => x.Account == this.Account);
            if(hasAccount)
            {
                string error = string.Format("账号 {0} 已经被使用", this.Account);
                throw new Exception(error);
            }
        }

        public Iworld.Author.Model.Author GetModel(IModelToDbContextOfAuthor db)
        {
            Iworld.Author.Model.Author ret = new Iworld.Author.Model.Author() {
                Account = this.Account,Username=this.Username,
                Password = EncryptHelper.EncryptByMd5(this.Password),
                TwoLevelPassword = EncryptHelper.EncryptByMd5(this.TwoLevelPassword),
                QQ=this.QQ,Phone=this.Phone,IdentityCard=this.IdentityCard
        };
        return ret;
       }

        public override void CheckAllowExecuteOrNot(ModelToDbContext db)
        {
           
        }
    }
}
